// import { FormatDisplayUtils } from '@/utils/formatdisplay_data'
import {NodeUtils} from "./nodeUtils.js";

const isEmptyArray = data => Array.isArray(data) ? data.length === 0 : true

export class FormatDisplayUtils {
    /**
     * 格式化显示数据
     * @param {Array} parmData
     * @returns Object
     */
    static getToTree(parmData) {
        if (!parmData.nodeList || !parmData.nodeList.length) {
            parmData.nodeList = [NodeUtils.createJudgeNode()]
        }
        let node = this.createNodeDisplay(parmData);
        let formatList = this.formatDisplayStructNodeList(parmData.nodeList);
        node.nodeConfig = this.depthConverterToTree(formatList);//parmData.nodes
        console.log(node)
        return node
    }

    /**
     * 创建Node Data 数据
     * @param { Object } nodeData - 源节点数据
     * @returns Object
     */
    static createNodeDisplay(nodeData) {
        return {
            ...nodeData,
            nodeConfig: {},
        }
    }
    /**
     * List 转成tree结构
     * @param {Array} parmData
     * @returns
     */
    static depthConverterToTree(parmData) {
        
        if (isEmptyArray(parmData)) return
        let nodesGroup = {}, startNode = {}
        for (let t of parmData) {
            if (nodesGroup.hasOwnProperty(t.nodeFrom)) {
                nodesGroup[t.nodeFrom].push(t)
            } else {
                nodesGroup[t.nodeFrom] = [t]
            }
        }
       
        for (let node of parmData) {
            if (1 === node.nodeType) {
                startNode = node;
            }
            Object.assign(node, { conditionExpressionList: [] });
            let currNodeId = node.nodeId;
            if (nodesGroup.hasOwnProperty(currNodeId)) {
                let itemNodes = nodesGroup[currNodeId];
                for (let itemNode of itemNodes) {
                    if (3 === itemNode.nodeType) {
                        node.conditionExpressionList.push(itemNode);
                    } else {
                        node.childNode = itemNode;
                    }
                }
            }
        }
        return startNode
    }

    static formatDisplayStructNodeList(nodeList) {
        if (isEmptyArray(nodeList)) return nodeList;
        for (let node of nodeList) {
            if (node.nodeType === 3) {
                let obj = JSON.parse(node.nodeObj)
                node.priorityLevel = obj.sort;
                node.isDefault = obj.isDefault;
                node.conditionList = obj.conditionList || [];
            }

            if (node.nodeType === 2 || node.nodeType === 5) {
                let obj = JSON.parse(node.nodeObj)
                // 按钮权限处理
                node.btnPermission= {
                    "$$$component&&&": '',
                }
                if (Array.isArray(node.permiList) && node.permiList.length>0){
                    node.permiList.forEach(item=>{
                        node.btnPermission[item.fieldId]=item.permi
                    })
                }
                if (obj && obj.vetoUsersList){
                    node.vetoUsersList=obj.vetoUsersList;
                }
                //  //1-指定成员，4-部门负责人，6-标签，8-申请人本人，9-申请人自选
                node.nodeApproveList= []
                if (["1","4","6"].includes(node.approvalUserType)){
                    if (node.approvalParams){
                        node.nodeApproveList=node.approvalParams.split(",").map((item) => Number.parseInt(item));
                    }
                }
                delete node.property;
            }
        }
        return nodeList;
    }
}
